﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IntillisenseLib;

namespace LittleSolution
{
    public class AppDoc
    {
        private static AppDoc instance;


        private ObjectRoot objRoot;

        public ObjectRoot ObjRoot
        {
            get { return objRoot; }            
        }
        
        private List<MessageTemplate> messageTemplates;

        public List<MessageTemplate> MessageTemplates
        {
            get { return messageTemplates; }
            set { messageTemplates = value; }
        }
        public AppDoc()
        {
            instance = this;        
            messageTemplates = new List<MessageTemplate>();
            objRoot = new ObjectRoot();
        }
        public static AppDoc GetInstance()
        {
            return instance;
        }
        public void LoadData()
        {

        }
        public void SaveData()
        {

        }
#region Objects Method
        public void AddObj(ObjectTemplete obj)
        {
            try
            {
                objRoot.AddObj(obj);
            }
            catch (System.Exception ex)
            {            
                throw ex;
            }
            
        }
        public void DeleteObj(ObjectTemplete obj)
        {
            objRoot.DeleteObj(obj);
        }
        public ObjectTemplete FindObject(String objectName, eObjectType type)
        {
            return objRoot.FindObject(objectName, type);
        }
        public void DeleteAllObject(eObjectType type)
        {
            objRoot.DeleteAllObject(type);
        }

#endregion
#region Message Method
        public void AddMessage(MessageTemplate msg)
        {
            if (messageTemplates == null)
            {
                messageTemplates = new List<MessageTemplate>();
            }
            if (messageTemplates.Where(m => m.MessageName == msg.MessageName).Count() > 0)
            {
                throw new Exception("The message template name is existed");
            }
            messageTemplates.Add(msg);
        }
        public void DeleteMessage(MessageTemplate msg)
        {
            if (messageTemplates == null)
            {
                return;
            }
            messageTemplates.Remove(msg);
        }
        public MessageTemplate FindMessage(String messageName)
        {
            return messageTemplates.Where(m => m.MessageName == messageName).FirstOrDefault();
        }
#endregion
       
    }
}
